package org.auth.config;

import org.auth.framework.CustomDaoAuthenticationProvider;
import org.auth.framework.CustomUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

	/**
	 * 自定义UserDetailsService对象
	 */
	@Autowired
	private CustomUserDetailsService customUserDetailsService;

    /**
     * 需要配置这个支持password模式
     */
    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
    
    /**
     * 自定义authenticationProvider对象
     */
	@Bean
	public AuthenticationProvider authenticationProvider() {
		return new CustomDaoAuthenticationProvider(customUserDetailsService);
	} 
    
	/**
     * 设定authenticationProvider对象
     */
    @Override  
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {  
        auth.authenticationProvider(authenticationProvider());  
    } 
}
